% read xls file
clear all
% close all
fclose all

path1='C:\Users\Tom�s\Desktop\Temp\';


fileName='CC.xlsx';
opts = spreadsheetImportOptions('Sheet',14)


opts = detectImportOptions([path1 fileName]);
preview([path1 fileName],opts)


% opts = setvaropts(opts,'TreatAsMissing','nan');
% opts = setvaropts(opts,'TreatAsMissing','NaN');
opts.PreserveVariableNames=1
opts.MissingRule = 'fill';
% opts.TreatAsMissing='nan';
opts.VariableNamesRange=1;

% opts = setvaropts(opts,{'spikelets'},'FillValue','nan');
% opts = setvaropts(opts,'FillValue','NaN');



M = readcell([path1 fileName],'NumHeaderLines',1);
for i=1:size(M,1)
    
%organize variable ID
    ID(i)=M{i,3};
    
%organize variable Vramp (0 or 1)
    if ischar(M{i,10})
        Vramp(i)=nan;
    else
        Vramp(i)=M{i,10};
    end
%organize variable spontFR
 if ischar(M{i,13})
        spontFR(i)=nan;
    else
        spontFR(i)=M{i,13};
 end
    
 %organize variable IPSCRate
  if ischar(M{i,14})
        IPSCRate(i)=nan;
    else
        IPSCRate(i)=M{i,14};
  end
  
%  %organize variable incMol
%   if ischar(M{i,11})
%         incMol(i)=nan;
%     else
%         incMol(i)=M{i,11};
%   end
%  
  %orgnaize variable Rm
   if ischar(M{i,11})
       Rm(i)=nan;
    else
        Rm(i)=M{i,11};
   end
    
 %organize variableCm
  if ischar(M{i,12})
        Cm(i)=nan;
    else
        Cm(i)=M{i,12};
  end
 
%   %organize variable APwidth
%    if ischar(M{i,27})
%         APwidth(i)=nan;
%     else
%         APwidth(i)=M{i,27};
%    end
 
%    %organize variable SomaPos
%     if ischar(M{i,16})
%         somaPos(i)=nan;
%     else
%         somaPos(i)=M{i,16};
%     end
 
%     %organize variable morph
%      if ischar(M{i,20});
%         morph(i)=nan;
%     else
%         morph(i)=M{i,20};
%      end
 
     %organize variable blockers
      if ischar(M{i,21});
        blockers(i)=nan;
    else
        blockers(i)=M{i,21};
      end
%  % organize variable spikelet variance
%    if ischar(M{i,33});
%         blockers(i)=nan;
%     else
%         blockers(i)=M{i,33};
%    end
%       
%    % organize variable Ih
%    if ischar(M{i,31});
%        Ih(i)=nan;
%    else
%        Ih(i)=M{i,31};
%    end
   
   %organize variable spikes
   if ischar(M{i,26});
       spikes(i,:)=nan;
   else
       for j=1:13
       spikes(i,j)=M{i,26+(j-1)};
       end
       
   end
   
      %organize variable person
   if strcmp('Tom�s',(M{i,2}));
       Tom(i)=1;
   else
       Tom(i)=0;     
   end
  
   
   
   
   
end

%% plot all shit

color1=[27 192 255]./255;
color1=[74, 181, 89]./255;
color2=[218 53 27]./255;
color2=[193, 62, 178]./255;

color3=[0 0 0];

alphaColors=0.5;

%only plot the cells that are confirmed for molecular ID 

blockers(isnan(blockers))=0;
Tom=logical(Tom);
SR=~Tom;

%% plot #2 
% input outupt curves

% classMat1=nan(numel(data),14);
% classMat2=nan(numel(data),14);
VSteps=[-30:5:30];

temp=[];
for i=ID(1:end-1)
%     plot(VSteps,spikes(i,:), 'color', [27 192 218 0.2*255]./255,'linewidth',1)
    temp=[temp;spikes(i,:)];
end

%remove NAN rows
% X(any(isnan(X), 2), :) = []; % This
CCIOMat=rmmissing(temp,1);
meanIO1=nanmean(temp,1);
% 
% figure;
% hold on
% plot(VSteps, meanIO1,'color', [27 192 218]./255,'linewidth',2)
% 
% 
% axis tight
% xlim([-30 100])
% set(gcf,'color','white');
% xlabel('Injected current (pA)')
% ylabel('number of spikes')
% set(gca,'FontSize', 12);
% set(gcf,'Position',[100 100 300 300])


%% shadederrorbar plot for IO curves
 figure
 x=VSteps;
 y1=CCIOMat;
%  y1=temp;
% shadedErrorBar(VSteps,mean(y1),std(y1)/sqrt(size(y1,1)),'lineprops','-b','transparent',1);
s=shadedErrorBar(VSteps,nanmean(y1,1),nanstd(y1)./sqrt(   sum(~isnan(y1),1) ) ,'lineprops',{'color', [27 192 255]./255});
s.mainLine.LineWidth = 2;
s.patch.FaceAlpha = 0.5;
 hold on;

 

axis tight
xlim([-10 30])
Ytick2=45;
ylim([0 Ytick2]);
set(gca, 'YTick', [0:Ytick2/3:Ytick2 ]);
yticklabels({0,'','',Ytick2})
set(gcf,'color','white');
set(gca,'color','white');
xlabel('injected current (pA)')
ylabel('action potentials')
set(gca,'FontSize', 12);
set(gcf,'Position',[100 100 300 300])



%% plot #3 
%Basic parameters

figure;
hold on

temp=Rm(Tom);

jitt=rand(numel(temp),1)/2;
% s4=scatter(1-1/6+jitt,temp,90'.','filled');
% s4.MarkerFaceColor=rgb('black');
% s4.MarkerFaceAlpha = 0.2;
s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
% s4.MarkerFaceAlpha = alphaColors;
s4.MarkerEdgeColor=[0 0 0];


scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([0 2000])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [0:1000:2000],'tickdir','out')

xticks({})
ylabel('Rm (MOhm)')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(Rm(Tom), 'symbol','', 'color','k')
ylim([0 2000])
ylabel('Rm (MOhm)')
set(gcf,'Position',[1240 238 173 480]);



%PLOT Cm

figure;
hold on

temp=Cm(Tom);

jitt=rand(numel(temp),1)/2;

s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
s4.MarkerEdgeColor=[0 0 0];

scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([0 50])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [0:25:50],'tickdir','out')

xticks({})
ylabel('Cm (pF)')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(Cm(Tom), 'symbol','', 'color','k')
ylim([0 50])
ylabel('Cm (pF)')
set(gcf,'Position',[1240 238 173 480]);


% plot resting potential

figure;
hold on

temp=Vramp(Tom);

jitt=rand(numel(temp),1)/2;

s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
s4.MarkerEdgeColor=[0 0 0];

scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([-90 -50])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [-90:20:-50],'tickdir','out')

xticks({})
ylabel('Resting potential (mV)')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(Vramp(Tom), 'symbol','', 'color','k')
ylim([-90 -50])
ylabel('Vm (mV)')
set(gcf,'Position',[1240 238 173 480]);

% plot spontaneous firing rate
figure;
hold on

temp=spontFR(Tom);

jitt=rand(numel(temp),1)/2;

s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
s4.MarkerEdgeColor=[0 0 0];

scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([0 20])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [0:10:20],'tickdir','out')

xticks({})
ylabel('Spontaneous firing (sp/s)')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(spontFR(Tom), 'symbol','', 'color','k')
ylim([0 20])
ylabel('firing rate (sp/s)')
set(gcf,'Position',[1240 238 173 480]);

%Plot IPSC rate

figure;
hold on

temp=IPSCRate(Tom);

jitt=rand(numel(temp),1)/2;

s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
s4.MarkerEdgeColor=[0 0 0];

scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([0 150])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [0 50 100 150],'tickdir','out')

xticks({})
ylabel('IPSC Frequency (s-1)')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(IPSCRate(Tom), 'symbol','', 'color','k')
ylim([0 150])
ylabel('IPSC Frequency (s-1)')
set(gcf,'Position',[1240 238 173 480]);

%% plot Ecl
Ecl=[-73.4 -92.7 -77.4 -76.9 -77.6 -78.9 -73.5];
figure;
hold on

temp=Ecl;

jitt=rand(numel(temp),1)/2;

s4=scatter(0-1/6+jitt,temp,100,'k','filled');
s4.MarkerFaceColor=[1 1 1];
s4.MarkerEdgeColor=[0 0 0];

scatter(0, nanmean(temp(:)),100,'k','filled','markerFaceColor','k')
tempSEM=nanstd(temp(:))/sqrt(sum(~isnan(temp)))
plot([0 0],[(nanmean(temp(:)-tempSEM)) (nanmean(temp(:))+tempSEM)],'color',[0 0 0],'linewidth',2)

xlim([-1 1])
ylim([-100 -60])
set(gcf,'color','white');
set(gca,'FontSize', 12,'fontname','Arial');
box off
set(gca, 'YTick', [-100 -80 -60],'tickdir','out')

xticks({})
ylabel('ECl (mV)')
set(gcf,'Position',[1240 238 173 480]);

figure
boxplot(Ecl, 'symbol','', 'color','k')
ylim([-100 -60])
ylabel('ECl (mV)')
set(gcf,'Position',[1240 238 173 480]);



